﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace 窗口顶部提示框
{
    /// <summary>
    /// CustomPopupExample.xaml 的交互逻辑
    /// </summary>
    public partial class CustomPopupExample : UserControl
    {
        public CustomPopupExample()
        {
            InitializeComponent();
            MessageBoxGrid.RenderTransform = new TranslateTransform();
        }

        // 显示提示框
        public void ShowMessage(string message)
        {
            // 设置提示框的消息
            MessageText.Text = message;

            // 设置提示框为可见
            MessageBoxGrid.Visibility = Visibility.Visible;

            // 获取并启动弹入动画
            Storyboard bounceInStoryboard = (Storyboard)this.Resources["EaseOutAnimation_Open"];
            bounceInStoryboard.Begin();
        }

        // 关闭提示框
        private void CloseMessage(object sender, RoutedEventArgs e)
        {
            // 确保 MessageBoxGrid 的 RenderTransform 只创建一次
            if (MessageBoxGrid.RenderTransform == null)
            {
                MessageBoxGrid.RenderTransform = new TranslateTransform();
            }


            Storyboard bounceInStoryboard = (Storyboard)this.Resources["EaseOutAnimation_Close"];

            bounceInStoryboard.Completed += (s, args) =>
            {
                MessageBoxGrid.Visibility = Visibility.Collapsed;
            };

            bounceInStoryboard.Begin();

            // 创建动画：向上滑出
            //            DoubleAnimation slideOutAnimation = new DoubleAnimation {
            //                From = 10, // 从当前显示位置开始
            //                To = -50, // 向上滑出屏幕，调整为负数，确保完全滑出
            //                Duration = new Duration(TimeSpan.FromSeconds(0.2))
            //            };

            // 将动画应用到提示框的 Y 轴
            //            TranslateTransform translateTransform = (TranslateTransform)MessageBoxGrid.RenderTransform;
            //            slideOutAnimation.Completed += (s, args) =>
            //            {
            //                MessageBoxGrid.Visibility = Visibility.Collapsed;
            //            };
            // translateTransform.BeginAnimation(TranslateTransform.YProperty, slideOutAnimation);
        }

    }
}
